package com.splic.api.controller.product;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.ShiroUtils;
import com.splic.product.domain.*;
import com.splic.product.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户健康数据
 */
@RestController
@RequestMapping("/api/userHealthy")
@Slf4j
public class ApiUserHealthyController  extends BaseController {


    @Autowired
    private UserBodyDataService userBodyDataServiceImpl;
    @Autowired
    private UserVisionDataService userVisionDataServiceImpl;
    @Autowired
    private UserBloodpressureDataService userBloodpressureDataServiceImpl;
    @Autowired
    private UserBloodfatDataService userBloodfatDataServiceImpl;
    @Autowired
    private UserBloodsugarDataService userBloodsugarDataServiceImpl;
    @Autowired
    private UserHeartrateDataService userHeartrateDataServiceImpl;
    @Autowired
    private UserPhysiologicalCycleDataService userPhysiologicalCycleDataServiceImpl;
    /**
     *  保存用户身体数据
     * @return
     */
    @PostMapping("/saveBodyData")
    public ResponseObj saveBodyData(@RequestBody UserBodyData userBodyData){
        try {
            if(userBodyData.getHeight() == null || userBodyData.getWeight() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userBodyData.setUserId(ShiroUtils.getUserId());
            int count = userBodyDataServiceImpl.insert(userBodyData);
            if(count <= 0){
                throw new BusinessException("保存用户身体数据失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存用户身体数据失败:{}",e);
            return ResponseObj.ERROR("保存用户身体数据失败");
        }
    }

    /**
     * 获取用户身体数据列表
     * @return
     */
    @GetMapping("/getBodyDataList")
    public ResponseObj<PageInfo<UserBodyData>> getBodyDataList(){
        try{
            startPage();
            UserBodyData userBodyData = new UserBodyData();
            userBodyData.setUserId(ShiroUtils.getUserId());
            List<UserBodyData> list = userBodyDataServiceImpl.selectUserBodyDataList(userBodyData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户身体数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户身体数据列表失败");
        }
    }


    /**
     *  保存用户视力数据
     * @return
     */
    @PostMapping("/saveVisionData")
    public ResponseObj saveVisionData(@RequestBody UserVisionData userVisionData) {
        try {
            if (userVisionData.getLeftEyeValue() == null || userVisionData.getRightEyeValue() == null) {
                return ResponseObj.ERROR("参数为空");
            }
            userVisionData.setUserId(ShiroUtils.getUserId());
            int count = userVisionDataServiceImpl.insert(userVisionData);
            if (count <= 0) {
                throw new BusinessException("保存用户视力数据失败");
            }
            return ResponseObj.SUCCESS();
        } catch (Exception e) {
            log.error("保存用户视力数据失败:{}", e);
            return ResponseObj.ERROR("保存用户视力数据失败");
        }
    }

    /**
     * 获取用户视力数据列表
     * @return
     */
    @GetMapping("/getVisionDataList")
    public ResponseObj<PageInfo<UserVisionData>> getVisionDataList(){
        try{
            startPage();
            UserVisionData userVisionData = new UserVisionData();
            userVisionData.setUserId(ShiroUtils.getUserId());
            List<UserVisionData> list = userVisionDataServiceImpl.selectUserVisionDataList(userVisionData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户视力数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户视力数据列表失败");
        }
    }


    /**
     *  保存用户血压数据
     * @return
     */
    @PostMapping("/saveBloodpressureData")
    public ResponseObj saveBloodpressureData(@RequestBody UserBloodpressureData userBloodpressureData){
        try {
            if(userBloodpressureData.getSystolicPressure() == null || userBloodpressureData.getDiastolicPressure() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userBloodpressureData.setUserId(ShiroUtils.getUserId());
            int count = userBloodpressureDataServiceImpl.insert(userBloodpressureData);
            if(count <= 0){
                throw new BusinessException("保存用户血压数据失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存用户血压数据失败:{}",e);
            return ResponseObj.ERROR("保存用户血压数据失败");
        }
    }

    /**
     * 获取用户血压数据列表
     * @return
     */
    @GetMapping("/getBloodpressureDataList")
    public ResponseObj<PageInfo<UserBloodpressureData>> getBloodpressureDataList(){
        try{
            startPage();
            UserBloodpressureData userBloodpressureData = new UserBloodpressureData();
            userBloodpressureData.setUserId(ShiroUtils.getUserId());
            List<UserBloodpressureData> list = userBloodpressureDataServiceImpl.selectUserBloodpressureDataList(userBloodpressureData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户血压数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户血压数据列表失败");
        }
    }


    /**
     *  保存用户血脂数据
     * @return
     */
    @PostMapping("/saveBloodfatData")
    public ResponseObj saveBloodfatData(@RequestBody UserBloodfatData userBloodfatData){
        try {
            if(userBloodfatData.getHighDensityLipoprotein() == null || userBloodfatData.getLowDensityLipoprotein() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userBloodfatData.setUserId(ShiroUtils.getUserId());
            int count = userBloodfatDataServiceImpl.insert(userBloodfatData);
            if(count <= 0){
                throw new BusinessException("保存用户血脂数据失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存用户血脂数据失败:{}",e);
            return ResponseObj.ERROR("保存用户血脂数据失败");
        }
    }

    /**
     * 获取用户血脂数据列表
     * @return
     */
    @GetMapping("/getBloodfatDataList")
    public ResponseObj<PageInfo<UserBloodfatData>> getBloodfatDataList(){
        try{
            startPage();
            UserBloodfatData userBloodfatData = new UserBloodfatData();
            userBloodfatData.setUserId(ShiroUtils.getUserId());
            List<UserBloodfatData> list = userBloodfatDataServiceImpl.selectUserBloodfatDataList(userBloodfatData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户血脂数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户血脂数据列表失败");
        }
    }


    /**
     *  保存用户血糖数据
     * @return
     */
    @PostMapping("/saveBloodsugarData")
    public ResponseObj saveBloodsugarData(@RequestBody UserBloodsugarData userBloodsugarData){
        try {
            if(userBloodsugarData.getMeasureValue() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userBloodsugarData.setUserId(ShiroUtils.getUserId());
            int count = userBloodsugarDataServiceImpl.insert(userBloodsugarData);
            if(count <= 0){
                throw new BusinessException("保存用户血糖数据失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存用户血糖数据失败:{}",e);
            return ResponseObj.ERROR("保存用户血糖数据失败");
        }
    }

    /**
     * 获取用户血糖数据列表
     * @return
     */
    @GetMapping("/getBloodsugarDataList")
    public ResponseObj<PageInfo<UserBloodsugarData>> getBloodsugarDataList(){
        try{
            startPage();
            UserBloodsugarData userBloodsugarData = new UserBloodsugarData();
            userBloodsugarData.setUserId(ShiroUtils.getUserId());
            List<UserBloodsugarData> list = userBloodsugarDataServiceImpl.selectUserBloodsugarDataList(userBloodsugarData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户血糖数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户血糖数据列表失败");
        }
    }


    /**
     *  保存用户心率数据
     * @return
     */
    @PostMapping("/saveHeartrateData")
    public ResponseObj saveHeartrateData(@RequestBody UserHeartrateData userHeartrateData){
        try {
            if(userHeartrateData.getMeasureValue() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userHeartrateData.setUserId(ShiroUtils.getUserId());
            int count = userHeartrateDataServiceImpl.insert(userHeartrateData);
            if(count <= 0){
                throw new BusinessException("保存用户心率数据失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存用户心率数据失败:{}",e);
            return ResponseObj.ERROR("保存用户心率数据失败");
        }
    }

    /**
     * 获取用户心率数据列表
     * @return
     */
    @GetMapping("/getHeartrateDataList")
    public ResponseObj<PageInfo<UserHeartrateData>> getHeartrateDataList(){
        try{
            startPage();
            UserHeartrateData userHeartrateData = new UserHeartrateData();
            userHeartrateData.setUserId(ShiroUtils.getUserId());
            List<UserHeartrateData> list = userHeartrateDataServiceImpl.selectUserHeartrateDataList(userHeartrateData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取用户心率数据列表失败:{}",e);
            return ResponseObj.ERROR("获取用户心率数据列表失败");
        }
    }


    /**
     *  保存女性生理周期数据
     * @return
     */
    @PostMapping("/savePhysiologicalCycleData")
    public ResponseObj savePhysiologicalCycleData(@RequestBody UserPhysiologicalCycleData userPhysiologicalCycleData){
        try {
            if(userPhysiologicalCycleData.getStartTime() == null || userPhysiologicalCycleData.getEndTime() == null){
                return ResponseObj.ERROR("参数为空");
            }
            userPhysiologicalCycleData.setUserId(ShiroUtils.getUserId());
            int count = userPhysiologicalCycleDataServiceImpl.insert(userPhysiologicalCycleData);
            if(count <= 0){
                throw new BusinessException("保存女性生理周期数据失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("保存女性生理周期数据失败:{}",e);
            return ResponseObj.ERROR("保存女性生理周期数据失败");
        }
    }

    /**
     * 获取女性生理周期数据列表
     * @return
     */
    @GetMapping("/getPhysiologicalCycleDataList")
    public ResponseObj<PageInfo<UserPhysiologicalCycleData>> getPhysiologicalCycleDataList(){
        try{
            startPage();
            UserPhysiologicalCycleData userPhysiologicalCycleData = new UserPhysiologicalCycleData();
            userPhysiologicalCycleData.setUserId(ShiroUtils.getUserId());
            List<UserPhysiologicalCycleData> list = userPhysiologicalCycleDataServiceImpl.selectUserPhysiologicalCycleDataList(userPhysiologicalCycleData);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取女性生理周期数据列表失败:{}",e);
            return ResponseObj.ERROR("获取女性生理周期数据列表失败");
        }
    }


}
